package cn.wenhaha.app.job.kit;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.wenhaha.app.mapping.bean.dto.FieldObj;
import cn.wenhaha.app.mapping.bean.dto.WriteObj;
import cn.wenhaha.sync.core.Join;
import cn.wenhaha.sync.core.Query;
import cn.wenhaha.sync.core.QueryType;
import cn.wenhaha.sync.core.Signal;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 配置常用
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-12-07 21:57
 */
public class ConfigKit {


    /**
     * <p>
     * 查看是否有配置同步条件
     * </p>
     *
     * @Author: Wyndem
     * @DateTime: 2022-12-07 21:39
     */
    public static boolean isConditionValue(List<FieldObj> fieldObjs) {
        if (CollUtil.isEmpty(fieldObjs)) {
            return false;
        }
        for (FieldObj field : fieldObjs) {
            boolean notEmpty = (
                    StrUtil.equals(field.getType(), "4") &&
                            StrUtil.isNotEmpty(field.getValue())

            ) ||
                    !StrUtil.equals(field.getType(), "4") &&
                            StrUtil.isNotEmpty(field.getType()) &&
                            StrUtil.isNotEmpty(field.getField()) &&
                            StrUtil.isNotEmpty(field.getSignal());
            if (!notEmpty) {
                return false;
            }

        }
        return true;
    }


    /**
     * <p>
     * 查看回写是否为空
     * </p>
     *
     * @Author: Wyndem
     * @DateTime: 2022-12-07 22:07
     */
    public static boolean isWriteValue(WriteObj writeObj) {

        return StrUtil.isNotEmpty(writeObj.getType()) &&
                StrUtil.isNotEmpty(writeObj.getField()) &&
                ObjectUtil.isNotEmpty(writeObj.getValue()) &&
                StrUtil.isNotEmpty(writeObj.getValue().toString());


    }




    /**
     * <p>
     * 获取同步条件
     *
     * </p>
     *
     * @Author: Wyndem
     * @DateTime: 2022-12-01 22:01
     */
    public static  List<Query> getCondition(List<FieldObj> condition) {
        return condition.stream().map(c -> Query.builder()
                .name(c.getField())
                .join(Join.findByJoin(c.getJoin()))
                .signal(Signal.findBySignal(c.getSignal()))
                .type(QueryType.findByQueryType(Integer.valueOf(c.getType())))
                .value(c.getValue()).build()).collect(Collectors.toList());
    }



}
